<template>
	<web-view v-if="url" :src="url" @message="bindMessage"></web-view>
</template>

<script>
	export default {
		data() {
			return {
				url: null,
				userId: "",
				ownerInfo: {},
				token: "",
			};
		},
		onLoad(options) {
			let userInfo = uni.getStorageSync("userInfo");
			if (userInfo) {
				this.userId = userInfo.userUuid;
			}
			if (options && options.ownerInfo) {
				this.ownerInfo = JSON.parse(options.ownerInfo);
			} else {
				this.$api.msg("未检测到用户信息")
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			}
			this.getverifyToken()
		},
		methods: {
			//获取跳转百度人脸核验的verifyToken
			getverifyToken() {
				let params = {}
				this.$api.request.verifyToken(params, (res) => {
					if (res.code == 200) {
						this.token = res.data;
						this.idcardSubmit()
					} else {
						this.$api.msg(res.msg)
					}
				})
			},
			idcardSubmit() {
				let params = {
					idName: this.ownerInfo.name,
					idNo: this.ownerInfo.idCard,
					verifyToken: this.token
				}
				this.$api.request.idcardSubmit(params, (res) => {
					if (res.code == 200) {
						try {
							let data = JSON.parse(res.data)
							if (data.success == true) {
								let returnUrl = "https://cdjjyl.com.cn/lezhiSamrtServerH5/test.html?token=" + this.token
								let baseUrl =
									"https://cdjjyl.com.cn/face/print/?token=" + this.token + "&successUrl=" +
									encodeURIComponent(returnUrl) +
									"&failedUrl=" + encodeURIComponent(returnUrl) + "";
								console.log(baseUrl)
								this.url = baseUrl;
							} else {
								uni.showModal({
									title: '提示',
									showCancel: false,
									content: data.message,
									success: (res) => {
										if (res.confirm) {
											uni.navigateBack()
										}
									}
								});
							}
						} catch (err) {
							this.$api.msg("信息校验失败，请确认信息后重新校验")
							setTimeout(() => {
								uni.navigateBack()
							}, 1500)
						}
					} else {
						this.$api.msg(res.msg)
					}
				})
			},
			bindMessage(e) {
				//如果通过消息机制，可以在这里处理数据，和进行页面跳转逻辑
				console.log('接收到的消息：' + JSON.stringify(e.detail.data));
			}
		}

	}
</script>

<style lang="scss">

</style>
